package deanandjames.ui;

import deanandjames.image.BackgroundManager;
import deanandjames.image.ImageLoader;
import deanandjames.image.PlayerImage;
import deanandjames.sprite.Avatar;
import deanandjames.sprite.BrickManager;
import deanandjames.sprite.FireBallSprite;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

public class Controller {
	private static final String BRICKS_INFO = "bricksInfo.txt";
	private final Avatar avatar;
	private final BackgroundManager backgrandManager;
	private final BrickManager bricksManager;
	private final PlayerImage explosionPlayer;
	private final FireBallSprite fireball;
	private volatile boolean isGameOver;
	private boolean isGameRunning;
	private volatile boolean isPaused;
	private boolean showExplosion;
	private boolean showHelp;
	private final int explosionWidth;
	private final int explosionHeight;
	private int xExplosion;
	private int yExplosion;

	public Controller(int PANEL_WIDTH, int PANEL_HEIGHT, ImageLoader imagesLoader, GamePanel gamePanel, long period) {
		showHelp = true;
		isPaused = true;
		isGameOver = false;
		showExplosion = false;

		final BufferedImage explosionImage = imagesLoader.getImage("explosion");
		explosionWidth = explosionImage.getWidth();
		explosionHeight = explosionImage.getHeight();

		bricksManager = new BrickManager(PANEL_WIDTH, PANEL_HEIGHT, BRICKS_INFO, imagesLoader);
		backgrandManager = new BackgroundManager(PANEL_WIDTH, PANEL_HEIGHT, bricksManager.getMoveSize(), imagesLoader);
		explosionPlayer = new PlayerImage("explosion", (int) (period / 1000000L), 0.5, false, imagesLoader);
		explosionPlayer.setWatcher(gamePanel);
		avatar = new Avatar(PANEL_WIDTH, PANEL_HEIGHT, bricksManager.getMoveSize(), bricksManager, imagesLoader, (int) (period / 1000000L));
		fireball = new FireBallSprite(PANEL_WIDTH, PANEL_HEIGHT, imagesLoader, gamePanel, avatar);
	}

	public void display(Graphics graphics) {
		backgrandManager.display(graphics);
		bricksManager.display(graphics);
		avatar.drawSprite(graphics);
		fireball.drawSprite(graphics);

	}

	public void drawExplosion(Graphics graphics) {
		graphics.drawImage(explosionPlayer.getCurrentImage(), xExplosion, yExplosion, null);
	}

	public void gameOver() {
		isGameOver = true;
	}

	public void gameUpdate() {
		if (!isPaused && !isGameOver) {
			if (avatar.willHitBrick()) {
				avatar.stayStill();
				bricksManager.stayStill();
				backgrandManager.stayStill();
			}
			backgrandManager.update();
			bricksManager.update();
			avatar.updateSprite();
			fireball.updateSprite();
			if (showExplosion) {
				explosionPlayer.update();
			}
		}
	}

	public int getExplosionHeight() {
		return explosionHeight;
	}

	public PlayerImage getExplosionPlayer() {
		return explosionPlayer;
	}

	public int getExplosionWidth() {
		return explosionWidth;
	}

	public void hideExplosion() {
		showExplosion = false;
	}

	public boolean isGameOver() {
		return isGameOver;
	}

	public boolean isGameRunning() {
		return isGameRunning;
	}

	public boolean isShowingExplosion() {
		return showExplosion;
	}

	public boolean isShowingHelp() {
		return showHelp;
	}

	public void pause() {
		isPaused = true;
	}

	public void processKey(KeyEvent e) {
		final int keyCode = e.getKeyCode();
		if (keyCode == KeyEvent.VK_ESCAPE || keyCode == KeyEvent.VK_Q || keyCode == KeyEvent.VK_END || keyCode == KeyEvent.VK_C && e.isControlDown()) {
			stopGame();
		}
		if (keyCode == KeyEvent.VK_H) {
			if (showHelp) {
				showHelp = false;
				isPaused = false;
			} else {
				showHelp = true;
				isPaused = true;
			}
		}
		if (!isPaused && !isGameOver) {
			if (keyCode == KeyEvent.VK_LEFT) {
				avatar.moveLeft();
				bricksManager.moveRight();
				backgrandManager.moveRight();
			} else if (keyCode == KeyEvent.VK_RIGHT) {
				avatar.moveRight();
				bricksManager.moveLeft();
				backgrandManager.moveLeft();
			} else if (keyCode == KeyEvent.VK_UP) {
				avatar.jump();
			} else if (keyCode == KeyEvent.VK_DOWN) {
				avatar.stayStill();
				bricksManager.stayStill();
				backgrandManager.stayStill();
			}
		}
	}

	public void restartExplosionImage() {
		explosionPlayer.restartAt(0);
	}

	public void setXExplosion(int xExplosion) {
		this.xExplosion = xExplosion;
	}

	public void setYExplosion(int yExplosion) {
		this.yExplosion = yExplosion;
	}

	public void showExplosion() {
		showExplosion = true;
	}

	public void startGame() {
		isGameRunning = true;
	}

	public void stopGame() {
		isGameRunning = false;
	}

	public void unpause() {
		isPaused = false;
	}
}
